package com.lgz.dbcourse.service.impl;


import com.lgz.dbcourse.dto.MovieSeatDTO;
import com.lgz.dbcourse.dto.ResultSet;
import com.lgz.dbcourse.entity.AudienceEntity;
import com.lgz.dbcourse.entity.MovieSeatEntity;
import com.lgz.dbcourse.mapper.AudienceMapper;
import com.lgz.dbcourse.mapper.MovieScheduleMapper;
import com.lgz.dbcourse.mapper.MovieSeatMapper;
import com.lgz.dbcourse.service.UserService;
import com.lgz.dbcourse.util.CommonUtil;
import com.lgz.dbcourse.util.DbUtil;

import java.util.Arrays;


/**
 * @Author: LGZ
 * @Date: 2021/4/10 19:23
 * @Description:
 */
public class UserServiceImpl implements UserService {

    @Override
    public ResultSet login(String phone) {
        AudienceEntity audienceEntity = new AudienceEntity();
        audienceEntity.setAudiencePhone(phone);
        AudienceMapper mapper = DbUtil.getSqlSession(true).getMapper(AudienceMapper.class);
        mapper.insertAudience(audienceEntity);
        return CommonUtil.resultReturn(mapper.getAudienceByPhone(audienceEntity));
    }

    @Override
    public ResultSet getSchedule() {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(MovieScheduleMapper.class).getSchedulePlaying());
    }

    @Override
    public ResultSet getSeat(MovieSeatDTO movieSeatDTO) {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(MovieSeatMapper.class).getSeat(CommonUtil.getMapperFactory().getMapperFacade().map(movieSeatDTO, MovieSeatEntity.class)));
    }

    @Override
    public ResultSet selectSeat(MovieSeatDTO[] movieSeatDTOS) {

        MovieSeatEntity[] list = new MovieSeatEntity[movieSeatDTOS.length];
        CommonUtil.getMapperFactory().getMapperFacade().mapAsArray(list, movieSeatDTOS, MovieSeatEntity.class);
//        System.out.println(list[0]);
        Arrays.asList(list).forEach(DbUtil.getSqlSession(true).getMapper(MovieSeatMapper.class)::updateSeat);

//        DbUtil.getSqlSession(true).getMapper(MovieSeatMapper.class).updateSeat(list[0]);
        return CommonUtil.resultReturn(null);
    }

    @Override
    public ResultSet getMyMovie(Long audienceId) {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(AudienceMapper.class).getMyMovie(audienceId));
    }
}
